package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LimitChooseRangePoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.*;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.WorkOrderOutstockEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 出库单
 *
 * @author Jian.Wang
 * @version v1.0
 * @date 2019/1/19
 * @since v1.0
 */
@Getter
public enum TempWorkOrderOutstockEnum {
    //
    BASE_INFO(WorkOrderOutstockEnum.BASE_INFO),
    SHEET_NO(WorkOrderOutstockEnum.SHEET_NO),
    TIME(WorkOrderOutstockEnum.TIME),
    REF_ID(WorkOrderOutstockEnum.REF_ID),
    WAREHOUSE_ID(WorkOrderOutstockEnum.WAREHOUSE_ID),
    PRODUCT_INFO(WorkOrderOutstockEnum.PRODUCT_INFO),
    PRODUCT(WorkOrderOutstockEnum.PRODUCT),
    EXTENSION_INFO(WorkOrderOutstockEnum.EXTENSION_INFO),
    LINK_CUSTOMER(WorkOrderOutstockEnum.LINK_CUSTOMER),
    LINK_CONTACT(WorkOrderOutstockEnum.LINK_CONTACT),
    CUSTOMER_PHONE(WorkOrderOutstockEnum.CUSTOMER_PHONE),
    CUSTOMER_ADDRESS(WorkOrderOutstockEnum.CUSTOMER_ADDRESS),
    EXPRESS_NAME(WorkOrderOutstockEnum.EXPRESS_NAME),
    EXPRESS_NO(WorkOrderOutstockEnum.EXPRESS_NO),
    MANAGER_ID(WorkOrderOutstockEnum.MANAGER_ID),
    MEMO(WorkOrderOutstockEnum.MEMO),
    SYSTEM_INFO(WorkOrderOutstockEnum.SYSTEM_INFO),
    CREATOR_ID(WorkOrderOutstockEnum.CREATOR_ID),
    ADD_TIME(WorkOrderOutstockEnum.ADD_TIME),
    UPDATE_TIME(WorkOrderOutstockEnum.UPDATE_TIME),
    TYPE(WorkOrderOutstockEnum.TYPE),
    OUTBOUND(WorkOrderOutstockEnum.OUTBOUND),
    WAREHOUSE_ID_LINKED_TEXT(WorkOrderOutstockEnum.WAREHOUSE_ID_LINKED_TEXT),
    REF_ID_LINKED_TEXT(WorkOrderOutstockEnum.REF_ID_LINKED_TEXT),
    LINK_CUSTOMER_HIDE(WorkOrderOutstockEnum.LINK_CUSTOMER_HIDE),
    LINK_CONTACT_HIDE(WorkOrderOutstockEnum.LINK_CONTACT_HIDE),
    ;
    private WorkOrderOutstockEnum anEnum;

    TempWorkOrderOutstockEnum(WorkOrderOutstockEnum outstockEnum) {
        this.anEnum = outstockEnum;
    }
    public static List<TempWorkOrderOutstockEnum> getAllEnum() {
        List<TempWorkOrderOutstockEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    // ===================================模板字段初始化======================================

    /**
     * 初始化编号
     * @param fieldAttrEntity
     */
    public static void initSheetNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        // 不能重复
        fieldAttrEntity.setNoRepeat(1);
        // 默认的编号规则
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.WORK_ORDER_OUTSTOCK_NO);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化出库类型
     *
     * @param fieldAttrEntity
     * @author chaoxiong.lei
     * @Date 11:01 2019/1/18
     * @since
     **/
    public static void initType(FieldAttrEntity fieldAttrEntity) {
        // 将出库类型设置为下拉选项
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(OutstockTypeEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化日期
     *
     * @param fieldAttrEntity
     */
    public static void initTime(FieldAttrEntity fieldAttrEntity) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化出库仓库
     * 将关联显示的字段设置到出库仓库字段中
     *
     * @param fieldAttrEntity
     * @author chaoxiong.lei
     * @Date 11:01 2019/1/18
     * @since
     **/
    public static void initWarehouseId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(WorkOrderOutstockEnum.WAREHOUSE_ID_LINKED_TEXT.getAttr());
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.WAREHOUSE.getCode());
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化负责人字段
     * 将关联显示的字段设置到该字段中
     *
     * @param fieldAttrEntity
     * @author chaoxiong.lei
     * @Date 11:01 2019/1/18
     * @since
     **/
    public static void initManagerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMultiple(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        // 默认的查询范围
        LimitChooseRangePoJo limitChooseRangePoJo = new LimitChooseRangePoJo();
        fieldAttrEntity.setLimitChooseRange(limitChooseRangePoJo);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
    }

    /**
     * 初始化关联单据字段
     * 将关联显示的字段设置到该字段中
     *
     * @param fieldAttrEntity
     * @author chaoxiong.lei
     * @Date 11:01 2019/1/18
     * @since
     **/
    public static void initRefId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(OutstockEnum.REF_ID_LINKED_TEXT.getAttr());
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.WORK_ORDER.getCode());
        fieldAttrEntity.setIsMultiBusiness(1);
        fieldAttrEntity.setBusinessType(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode());
        fieldAttrEntity.setIsForbidden(0);
        // 工单出库单的关联工单的关联字段去掉
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias()));
//        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
//        saasParticularAttributePoJo.setRelyAttr(LINK_CUSTOMER.anEnum.getAttr());
//        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
//        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }


    /**
     * 物流公司
     */
    public static void initExpressNo(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setComboType(ComboTypeEnum.CUSTOM.getType());
        fieldAttrEntity.setDictionaryFlag(0);
        fieldAttrEntity.setIsDictionary(1);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initProducts(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
    }

    public static void initLinkCustomer(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(LINK_CUSTOMER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setEditForbidden(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias()));
    }

    public static void initLinkContact(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(LINK_CONTACT_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias()));
    }

    public static void initCustomerPhone(FieldAttrEntity fieldAttrEntity){
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity type = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 0);
//        String str = "工作,手机,住址,主要,传真,其他";
        List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItemByEnum(PhoneExtEnum.class, FieldTypeEnum.COMBO.getType());
        itemPoJoList.get(0).setChecked(true);
        type.setItems(itemPoJoList);
        type.setRequired(0);
        type.setDictionaryFlag(1);
        type.setParentAttr(CUSTOMER_PHONE.anEnum.getAttr());
        type.setShowEditable(0);
        items.add(type);
        FieldAttrEntity phone = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 0);
        phone.setShowEditable(0);
        phone.setParentAttr(CUSTOMER_PHONE.anEnum.getAttr());
        phone.setRequired(0);
        items.add(phone);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setDictionaryFlag(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initAddress(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
    }
}
